<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<HTML>

<!-- Mirrored from bos.asmhackers.net/docs/mouse/docs/mouse.html by HTTrack Website Copier/3.x [XR&CO'2013], Tue, 07 Oct 2014 11:24:06 GMT -->
<HEAD>
<TITLE>PC mouse info</TITLE>
<meta name="description" content="PC mouse hardware information.">
<meta name="keywords" content="PC, mouse, hardware, protocol, serial, port, pin, PS/2, bus, programming, driver, data, packet, DOS, Genius, Logitech, reference, guide">
<meta name="distribution" content="global">
<meta name="copyright" content="1995-1998 Tomi Engdahl">
<meta name="author" contents="Tomi Engdahl">
<meta name="GENERATOR" content="Emacs">
</HEAD>
<BODY> 
<H1>PC mouse information</H1>
<P>
This documents tries to tell how PC mouse hardware works and how
to read it at the lowest level. If you are programming under some
ready made operating systems, then it would be better to use the
routines supplied by the mouse driver (a good description of DOS mouse driver
programming is available at <A HREF="ftp://x2ftp.oulu.fi/pub/msdos/programming/docs/gmouse.doc">ftp://x2ftp.oulu.fi/pub/msdos/programming/docs/gmouse.doc</A> in ascii format). There is also a good tutorial <A HREF="http://www.geocities.com/SiliconValley/2151/mouse.html">Programming the Microsoft Mouse</A> in the <A HREF="http://www.geocities.com/SiliconValley/2151/pcgpe.html">PC Game Programmer's Encyclopedia</A>.
<P>

<H2>How mouse system works</H2>
<P>
Typical PC mouse controlling system has the following parts:
<PRE>
sensors -> mouse controller -> communication link -> data interface -> driver -> software
</PRE>
Sensors are the movement detectors (typically optomechanical) which sense the mouse
movement and button swiches which sense the button states. Mouse controller reads
the state of those sensors and takes acount of current mouse position. When this information
changes the mouse controller sends a packet of data to the computer data interface controller.
<P>
The mouse driver in the computer received that data packet and decodes the information
from it and does actions based on the information. Typically mouse driver has the information
of the current mouse state (position and button states) and tells them to the
application or operating when it asks them. Typically the mouse drive calls mouse
cursor moving routines when mouse is moved and sends messages to the software when
buttons are pressed. 
<P>
In typical modern PC mouse driver the actual cursor movement is not linearly related
to the mouse movement. This might sound a bit strange but it has been found that
there are better ways to change the mouse movement to cursor muvement than just
simply causing one mouse step to move the cursor one pixel.
Xerox was the company which started development of mouse and graphical user
interfaces. Apple took many ideas from Xerox and developed them further in their
products.
During the pioneering research done at Xerox and Apple in the devellopment of
the graphical user interface (GUI), it became apparent that no particular
ratio between mouse movement and cursor movement was best suited for all tasks.
<P>
Early work detected that
there are two basic movements in the use of pointing devices: move cursor to
desired area and then exactly to desired target. Those two movements have contradictory
requirements, so Apple solved the problem by monitoring the mouse movements and change
it's CPI (counts per inch) characteristics. When the mouse was moved slowly it
remained 100 CPI and when the mouse was moved fast it appears to be 400 CPI mouse.
This method of adjusting CPI based on its usage has now been adopted by the default driver
in Windows 95 and is now the most commonly accepted way of translating mouse movements
to cursor screen movement
<P>
<HR>
<H1>Mouse hardware information</H1>

<H2>	Serial mouse</H2>

<H4>Voltage levels:</H4>
<P>
Mouse takes standard RS-232C output signals (+-12V) as its input signals.
Those outputs are in +12V when mouse is operated. Mouse takes some current
from each of the RS-232C port output lines it is connected (about 10mA).
Mouse send data to computer in levels that RS-232C receiver chip in the 
computer can uderstand as RS-232C input levels. Mouse outputs are normally
something like +-5V, 0..5V or sometimes +-12V. Mouse electronics
normally use +5V voltage.
<P>
<H4>Hardware implementation</H4>
<P>
PC serial mouse uses typically DRT and RTS lines for generating +5V power for microcontroller circuit in the
mouse. Because typical optomechanical mouse also needs power for 4 leds in the optocoupler movevement
detectors, there is not much power to loose. A typical approach is to use diodes to take current fron DTR and
RTS lines and then feed it through resistor to all of the (infrared) leds in the movement detectors. All four (infrared)
leds are connected in series, which gives about +5V voltage drop over all leds (typical to leds used in moused).
This +5V is adequate power for low power mouse microcontroller. The serial data transimitting circuit consists of
simple discrete transistor circuir to make it consume as little power as possible. The positive power supply usually
taken from RTS and DRT lines (just after the diodes and before the resistor going to leds). The negative supply for
transmitter is taken from TD pin. Typical PC serial port mouse takes 10 mA total current and operates at voltage
range of 6-15V. The data itself in sent using standard asuncronous RS-232C serial format:
<PRE>
              Start D0  D1  D2  D3  D4  D5  D6  D7  Stop
   Logic 0      ___ ___ ___ ___ ___ ___ ___ ___ ___
  +3..+15V     |   |   |   |   |   |   |   |   |   |
               |   |   |   |   |   |   |   |   |   |
               |   |   |   |   |   |   |   |   |   |
   Logic 1     |   |   |   |   |   |   |   |   |   |
  -3..-15V  ___|   |___|___|___|___|___|___|___|___|____

</PRE>

<P>

<H3>Microsoft serial mouse</H3>
<P>
Microsoft serial mouse is the most popular 2 button serial mouse type.
Typically that cheap tho button mouse which comes with the computer is
Microsoft mouse system. Microsoft mouse is supported in all major
operating systems.
<P>
<H4>Mouse resolution and tracking rate</H4>
<P>
Maximum tracking rate for Microsoft mouse is 40 reports/second * 127 counts per report
= 5080 counts per second. The most common range for typical mouses is 100 to 400 CPI (count per inch) but can be up to 1000 CPI (cheap ones typically are 100 CPI or 200 CPI models).
This means that you can move 100 CPI mouse up to speed of 50.8 inches per second
and 400 CPI mouse maximally at 12.7 inches per second. The actual accuracy of movement the
software sees is detemined by the settings of the mouse driver (many mouse drivers
have option to adjust mouse sensitivity).
<P>
<H4>Pinout</H4>
<PRE>
9 pin    25 pin     Wire Name            Comments
shell     1         Protective Ground
3         2         TD                  Serial data from host to mouse (only for power)
2         3         RD                  Serial data from mouse to host
7         4         RTS                 Positive voltage to mouse
8         5         CTS
6         6         DSR
5         7         Signal Ground
4         20        DTR                 Positive voltage to mouse and reset/detection

RTS = Request to Send   CTS = Clear to Send
DSR = Data Set Ready    DTR = Data Terminal Ready
</PRE>
To function correctly, both the RTS and DTR lines must be positive.  The
lines DTR-DSR and RTS-CTS must NOT be shorted.  Implement the RTS toggle
function by setting the RTS line negative and positive again.  The
negative pulse width is at least 100ms.  After a cold boot, the RTS line
is usually set to a negative level.  In this case, setting the RTS line to
a positive level is also considered an RTS toggle.
<P>

<H4>Serial data parameters:</H4>
1200bps, 7 databits, 1 stop-bit<BR>

<H4>Data packet format:</H4>

Data packet is 3 byte packet. It is send to the computer every time
mouse state changes (mouse moves or keys are pressed/released).
<PRE>
        D7      D6      D5      D4      D3      D2      D1      D0
 
1.      X       1       LB      RB      Y7      Y6      X7      X6
2.      X       0       X5      X4      X3      X2      X1      X0      
3.      X       0       Y5      Y4      Y3      Y2      Y1      Y0
</PRE>
Note: The bit marked with X is 0 if the mouse received with
7 databits and 2 stop bits format. It is also possible to use
8 databits and 1 stop bit format for receiving. In this case
X gets value 1. The safest thing to get everything working is
to use 7 databits and 1 stopbit when receiving mouse information
(and if you are making mouse then send out 7 databits and 2 stop bits).
<P>
The byte marked with 1. is send first, then the others. The bit D6
in the first byte is used for syncronizing the software to mouse
packets if it goes out of sync.
<PRE>
LB is the state of the left button (1 means pressed down)
RB is the state of the right button (1 means pressed down)
X7-X0 movement in X direction since last packet (signed byte)
Y7-Y0 movement in Y direction since last packet (signed byte)
</PRE>
<H4>Graphical description how the data is contained in the packet</H4>
<PRE>
              1st byte        2nd byte         3rd byte
          ================  ===============  ================
           - 1 ? ? Y Y X X  - 0 X X X X X X  - 0 Y Y Y Y Y Y
          ================  ===============  ================
               | | \ / \ /      \---------/      \---------/
               | |  |   |            |                |
               | |  |   \----\       |                |
               | |  \--------|-------|--------\       |
               | |          / \ /---------\  / \ /---------\
               | |         ================ =================
               | |          0 0 0 0 0 0 0 0  0 0 0 0 0 0 0 0
 Left Button --/ |         ================ =================
Right Button ----/            X increment      Y increment
</PRE>
<H4>Mouse identification</H4>
When DTR line is toggled, mouse should
send one data byte containing letter 'M' (ascii 77).
<P>
<H4>Logitech extension to protocol</H4>
<P>
Logitech uses this same protocol in their mouses (for example
Logitech Pilot mouse and others). The origianal protocol supports only
two buttons, but logitech as added third button to some of their
mouse models. To make this possible logitech has made one extension to the
protocol.
<P>
I have not seen any documentation about the exact documents, but here is
what I have found out:
The information of the third button state is sent using one extra byte
which is send after the normal packet when needed. Value 32 (dec) is
sent every time when the center button is pressed down. It is also
sent every time with the data packet when center button is kept
down and the mouse data packet is sent for other reasons. When
center button is released, the mouse sends the normal data packet followed
by data bythe which has value 0 (dec). As you can see the extra data
byte is sent only when you mess with the center button.
<P>
<H3>Mouse systems mouse</H3>

<H4>Serial data parameters:</H4>
1200bps, 8 databits, 1 stop-bit<BR>

<H4>The data is sent in 5 byte packets in following format:</H4>
<PRE>
        D7      D6      D5      D4      D3      D2      D1      D0

1.      1       0       0       0       0       LB      CB      RB
2.      X7      X6      X5      X4      X3      X2      X1      X0
3.      Y7      Y6      Y5      Y4      Y3      Y4      Y1      Y0
4.      X7'     X6'     X5'     X4'     X3'     X2'     X1'     X0'
5.      Y7'     Y6'     Y5'     Y4'     Y3'     Y4'     Y1'     Y0'

LB is left button state (0=pressed, 1=released)
CB is center button state (0=pressed, 1=released)
RB is right button state (0=pressed, 1=released)
X7-X0 movement in X direction since last packet in signed byte 
      format (-128..+127), positive direction right
Y7-Y0 movement in Y direction since last packet in signed byte 
      format (-128..+127), positive direction up
X7'-X0' movement in X direction since sending of X7-X0 packet in signed byte 
      format (-128..+127), positive direction right
Y7'-Y0' movement in Y direction since sending of Y7-Y0 in signed byte 
      format (-128..+127), positive direction up
</PRE>
The last two bytes in the packet (bytes 4 and 5) contains
information about movement data changes which have occured
after data butes 2 and 3 have been sent.
<P>
<HR>
<H2>Bus mouse</H2>
<P>
Bus mouse is connected to the PC using a mouse card put to ISA bus.
The card has the intelligence in it and the mouse has only the detectors
for mouse movement and mosue button switches.
The mouse is connected to card using 9 pin Hosiden connector.
<P>
<H4>Connector pinout</H4>
(the pin umbering might be wrong in this)
<PRE>
        1 2 3

       4 5 6 7

         8 9


1.      SW2
2.      SW3
3.      Signal Ground
4.      XB
5.      YA
6.      YB
7.      SW1
8.      +5V
9.      XA
</PRE>
SW1, SW2 and SW3 are signals from mouse buttons.<BR>
XA, XB, YA and YB are quadriture signals from mouse movement detectors.<BR>
<P>
<H4>Another connector numbering scheme</H4>
<PRE>

         7 8 9

        3 4 5 6

          1 2

 1.      +5v
 2.      XA  (H-Pulse)
 3.      XB  (HQ-Pulse)
 4.      YA  (V-Pulse)
 5.      YB  (VQ-Pulse)
 6.      SW1 (Left)
 7.      SW2 (Middle)
 8.      SW3 (Right)
 9.      GND
 SW1, SW2 and SW3 are signals from mouse buttons.XA, XB, YA and YB are
 quadriture signals from mouse movement detectors.
</PRE>

<H4>Wiring according logitech documentation</H4>
<P>
Pinout:
<PRE>
         7 8 9

        3 4 5 6

          1 2
</PRE>
Pin functions:
<PRE>
Wire     Mini-DIN       Logitech             Microsoft
Color      Pin       P-Series Signal       InPort Signal
Black       1             +5V                  +5V
Brown       2              X2                   XA
Red         3              X1                   XB
Orange      4              Y1                   YA
Yellow      5              Y2                   YB
Green       6              Left                 SW1
Violet      7              Middle               SW2
Gray        8              Right                SW3
White       9              GND                  Logic GND
SHIELD    shell            chassis              chassis
</PRE>
<HR>
<H2>PS/2 mouse</H2>
<P>
PS/2 mouse is connected to computer motherboard using same type of
6 pin connector as PS/2 keyboard. The data is sent using syncronous
serial protocol similar to protocol used by PS/2 keyboard.
The mouse data is handled using keyboard controller.
PS/2 type mouse is becoming more and more common because many new computers
(nearly all big brands and new no brand computers made using ATX standard motherboards)
have PS/2 mouse port integrated it in it and using PS/2 mouse connected to that port
also frees one serial port for other uses.
<P>
<H4>Mouse resolution and tracking rate</H4>
<P>
The standard PS/2 mouse (with Logitech mouse) defaults to 160 CPI and can be switched
to 40, 80, 160 or 320 CPI with software. Microsoft mouse driver for Windows 3.x and
Windows 95 defauls to 160 couns per inch. The maximum tracking rate for PS/2 mouse is
40 report/second * 255 counts per report = 10200 counts per second. For 100 CPI mouse
this would indicate maximum trackign rate of 102 inches per second and for 400 CPI mouse
only 25.2 inches per second.
<P>
<H4>Connector pinout</H4>
<P>
Pin     Wire Name
1       DATA
2       Reserved
3       Ground
4       +5V Supply
5       CLK
6       Reserved
Shield  Chassis
<P>
<H4>Data packet format</H4>
<PRE>
        D7      D6      D5      D4      D3      D2      D1      D0
1.      XV      XV      YS      XS      1       0       R       L
2.      X7      X6      X5      X4      X3      X2      X1      X0
3.      Y7      Y6      Y5      Y4      Y3      Y2      Y1      Y0

L       Left button state (1 = pressed down)
R       Right button state (1 = pressed down)
X0-X7   Movement in X direction
Y0-Y7   Movement in Y direction
XS,YS   Movement data sign bits (1 = negative)
XV,YV   Movement data overflow bits (1 = overflow has occured)
</PRE>
<P>
<H4>Physical connector</H4>
<P>
The PS/2 mouse connector has the following pinout when looking at
the connector on the back of the computer:
<PRE>
6 Pin PS2

1   DATA
2   No connection
3   GND
4   +5V
5   CLK
6   No connection

The PS2 is numbered as follows:
        
      6   5
     4     3
       2 1
</PRE>
<P>
Bi-directional transmission is controlled by the CLK and DATA lines.  Both
are fed by an open collector device which lets either host or mouse force
the line to "0".  During non-transmission, CLK is at "1" and  DATA can be
at "0" or "1".
<P>
The host can inhibit mouse transmission by forcing CLK to "0".  If the
host inhibits the mouse while it is transmitting, the byte must be
retransmitted (if the inhibit state arrived before the 11th clock).
<P>
There is a simple description in my old IBM PS/2 model 50/60 technical
reference. The mouse interface is the same as the keyboard interface.
The Intel 8042 supports two channels, one for keyboard and one for
the auxiliary device (mouse, trackball, touchpad).
Pin 1 is the data, pin3 ground, pin 4 +5V and pin 5 clock.
<P>
Receiving data: Check 'clock'. If inactive, there is a bit on the 'data'
line. Each transmission unit is one start bit, eight data bits, odd parity
and one stop bit. Start bits are low, stop bits high.
Each clock active or inactive period is 30 to 50 microseconds.
Data transition to falling edge of clock is 5 to 25 microseconds.
<P>
Sending: Check that both clock and data are high. Pull down
data for start bit, and start clocking.
<P>
NOTE: Logitech has made their own extension also to this protocol
to allow three buttons.
<P>
<HR>
<H2>Mouse movement detectors</H2>
<P>
The most tyical construction of mouse movement detectors are optomechanical
detectors. The system works so that X and Y movement of the mouse
rotates slotted disks. The movement info is then picked from those slotted
disks using optical sensors. There are two sensors for each slotted disk
and those sensors give out two singals which are 90 degreed
out of phase compared to each other.
<PRE>

sensor #0 ----    ----    ----    ----   light
              ----    ----    ----       no light

sensor #1   ----    ----    ----    --   light
          --    ----    ----    ----    no light
</PRE>
So if we call the getting light phase '1', and the not
getting light phase (when the light is blocked by the
wheel) '0' then it looks like this:
<PRE>
sensor #0 - 1111000011110000111100001111
sensor #1 - 0011110000111100001111000011
            ^^^^^^^^
            full cycle of pulses
</PRE>
This can be decoded into four discrete bit pairs for the two sensors:
<PRE>
         Forward direction     Backwards direction
            0 1 2 3                3 2 1 0
sensor #0 - 1 1 0 0                0 0 1 1
sensor #1 - 0 1 1 0                0 1 1 0
</PRE>
This information can be easily decoded usign small microcontroller
and then sent to computer in correct format.
<P>
<H2>Information sources</H2>
<UL>
  <LI>PC Magazine May 28, 1991
  <LI>Various articles form usenet news (the information later verified with measurement or other sources)
  <LI><A HREF="ftp://x2ftp.oulu.fi/pub/msdos/programming/docs/gmouse.doc">Programmer's Reference to Genius Mouse</A>
  <LI>My own experimenting with Genius Mouse GM6 ja Logitech Pilot Mouse
  <LI>Logitech Pointing Device Hardware Information Product Support Document # 1410
  <LI><A HREF="http://www.4qd.co.uk/faq/meece.html">Mice: How do they work?</A> by <A HREF="mailto:4qd@argonet.co.uk">Richard Torrens</A>
  <LI>In Pursuit Of The Perfect Portable Pointer by Intelink Electronics from EDN Products Edition April 16, 1977 pages 43-45
</UL>
<P>
<HR>
<ADDRESS><A HREF="http://www.hut.fi/~then/">Tomi Engdahl</A> &lt;then@delta.hut.fi&gt;</ADDRESS>

</BODY>

<!-- Mirrored from bos.asmhackers.net/docs/mouse/docs/mouse.html by HTTrack Website Copier/3.x [XR&CO'2013], Tue, 07 Oct 2014 11:24:06 GMT -->
</HTML>

